package com.jacoblyu.factoryrunner.ui.views

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material.icons.filled.Home
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import androidx.compose.ui.platform.LocalContext
import com.jacoblyu.factoryrunner.data.TestResultManager
import com.jacoblyu.factoryrunner.ui.theme.FactoryrunnerTheme

@Composable
fun USBTestView(navController: NavController) {
    // 获取屏幕配置信息
    val configuration = LocalConfiguration.current
    val screenWidthDp = configuration.screenWidthDp.dp
    val screenHeightDp = configuration.screenHeightDp.dp
    
    // 根据屏幕尺寸计算比例
    val isLargeScreen = screenWidthDp.value > 1500 // 判断是否为1920x1200分辨率
    
    // 根据屏幕大小调整字体和间距
    val titleFontSize = if (isLargeScreen) 36.sp else 28.sp
    val contentFontSize = if (isLargeScreen) 32.sp else 24.sp
    val buttonFontSize = if (isLargeScreen) 26.sp else 20.sp
    val buttonHeight = if (isLargeScreen) 80.dp else 60.dp
    val buttonWidth = if (isLargeScreen) 200.dp else 150.dp
    val iconSize = if (isLargeScreen) 48.dp else 36.dp
    
    // 初始化TestResultManager
    val context = LocalContext.current
    val testResultManager = remember { TestResultManager(context) }
    
    // USB测试状态
    var usbTestResult by remember { mutableStateOf(testResultManager.getTestResult("USB测试")) }
    
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0xFFF5F5F5))
    ) {
        // 顶部导航栏
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            // Home按钮
            IconButton(
                onClick = {
                    navController.navigate("home")
                },
                modifier = Modifier.size(iconSize)
            ) {
                Icon(
                    imageVector = Icons.Filled.Home,
                    contentDescription = "返回主页",
                    tint = Color(0xFF1976D2),
                    modifier = Modifier.size(iconSize * 0.7f)
                )
            }
            
            // 标题
            Text(
                text = "USB测试",
                fontSize = titleFontSize,
                fontWeight = FontWeight.Bold,
                color = Color(0xFF1976D2)
            )
            
            // 下一步按钮
            IconButton(
                onClick = {
                    // 导航到下一个测试页面
                    navController.navigate("screen_test")
                },
                modifier = Modifier.size(iconSize)
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowForward,
                    contentDescription = "下一步",
                    tint = Color(0xFF1976D2),
                    modifier = Modifier.size(iconSize * 0.7f)
                )
            }
        }
        
        // 中间内容区域
        Column(
            modifier = Modifier
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // USB测试文本
            Text(
                text = "USB测试",
                fontSize = contentFontSize,
                fontWeight = FontWeight.Medium,
                color = Color.Black,
                modifier = Modifier.padding(bottom = 40.dp)
            )
            
            // 通过和不通过按钮
            Row(
                horizontalArrangement = Arrangement.spacedBy(40.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 通过按钮
                Button(
                    onClick = {
                        // 保存测试结果到TestResultManager
                        testResultManager.saveTestResult("USB测试", "通过")
                        usbTestResult = "通过"
                        // 导航到下一个测试页面
                        navController.navigate("screen_test?result=通过&test=USB测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = if (usbTestResult == "通过") Color(0xFF4CAF50) else Color.Gray
                    )
                ) {
                    Text(
                        text = "通过",
                        fontSize = buttonFontSize,
                        color = Color.White,
                        fontWeight = FontWeight.Bold
                    )
                }
                
                // 不通过按钮
                Button(
                    onClick = {
                        // 保存测试结果到TestResultManager
                        testResultManager.saveTestResult("USB测试", "不通过")
                        usbTestResult = "不通过"
                        // 导航到下一个测试页面
                        navController.navigate("screen_test?result=不通过&test=USB测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = if (usbTestResult == "不通过") Color(0xFFF44336) else Color.Gray
                    )
                ) {
                    Text(
                        text = "不通过",
                        fontSize = buttonFontSize,
                        color = Color.White,
                        fontWeight = FontWeight.Bold
                    )
                }
            }
        }
    }
}

@Preview(showBackground = true, widthDp = 1280, heightDp = 800)
@Composable
fun USBTestPreview() {
    FactoryrunnerTheme {
        val navController = rememberNavController()
        USBTestView(navController = navController)
    }
}

@Preview(showBackground = true, widthDp = 1920, heightDp = 1200)
@Composable
fun USBTestLargePreview() {
    FactoryrunnerTheme {
        val navController = rememberNavController()
        USBTestView(navController = navController)
    }
}